home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / flex / flexs237.zoo / misc.c < prev    next >
C/C++ Source or Header  |  1990-09-03  |  15KB  |  827 lines

  1. /* misc - miscellaneous flex routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] =
  31.     "@(#) $Header: /usr/fsys/odin/a/vern/flex/RCS/misc.c,v 2.9 90/08/14 00:10:24 vern Exp $ (LBL)";
  32. #endif
  33.  
  34. #include <ctype.h>
  35. #include "flexdef.h"
  36.  
  37.  
  38. /* ANSI C does not guarantee that isascii() is defined */
  39. #ifndef isascii
  40. #define isascii(c) ((c) <= 0177)
  41. #endif
  42.  
  43.  
  44.  
  45. /* declare functions that have forward references */
  46.  
  47. void dataflush PROTO(());
  48. int otoi PROTO((Char []));
  49.  
  50.  
  51. /* action_out - write the actions from the temporary file to lex.yy.c
  52.  *
  53.  * synopsis
  54.  *     action_out();
  55.  *
  56.  *     Copies the action file up to %% (or end-of-file) to lex.yy.c
  57.  */
  58.  
  59. void action_out()
  60.  
  61.     {
  62.     char buf[MAXLINE];
  63.  
  64.     while ( fgets( buf, MAXLINE, temp_action_file ) != NULL )
  65.     if ( buf[0] == '%' && buf[1] == '%' )
  66.         break;
  67.     else
  68.         fputs( buf, stdout );
  69.     }
  70.  
  71.  
  72. /* allocate_array - allocate memory for an integer array of the given size */
  73.  
  74. void *allocate_array( size, element_size )
  75. int size, element_size;
  76.  
  77.     {
  78.     register void *mem;
  79.  
  80.     /* on 16-bit int machines (e.g., 80286) we might be trying to
  81.      * allocate more than a signed int can hold, and that won't
  82.      * work.  Cheap test:
  83.      */
  84.     if ( element_size * size <= 0 )
  85.         flexfatal( "request for < 1 byte in allocate_array()" );
  86.  
  87.     mem = (void *) malloc( (unsigned) (element_size * size) );
  88.  
  89.     if ( mem == NULL )
  90.     flexfatal( "memory allocation failed in allocate_array()" );
  91.  
  92.     return ( mem );
  93.     }
  94.  
  95.  
  96. /* all_lower - true if a string is all lower-case
  97.  *
  98.  * synopsis:
  99.  *    Char *str;
  100.  *    int all_lower();
  101.  *    true/false = all_lower( str );
  102.  */
  103.  
  104. int all_lower( str )
  105. register Char *str;
  106.  
  107.     {
  108.     while ( *str )
  109.     {
  110.     if ( ! isascii( *str ) || ! islower( *str ) )
  111.         return ( 0 );
  112.     ++str;
  113.     }
  114.  
  115.     return ( 1 );
  116.     }
  117.  
  118.  
  119. /* all_upper - true if a string is all upper-case
  120.  *
  121.  * synopsis:
  122.  *    Char *str;
  123.  *    int all_upper();
  124.  *    true/false = all_upper( str );
  125.  */
  126.  
  127. int all_upper( str )
  128. register Char *str;
  129.  
  130.     {
  131.     while ( *str )
  132.     {
  133.     if ( ! isascii( *str ) || ! isupper( (char) *str ) )
  134.         return ( 0 );
  135.     ++str;
  136.     }
  137.  
  138.     return ( 1 );
  139.     }
  140.  
  141.  
  142. /* bubble - bubble sort an integer array in increasing order
  143.  *
  144.  * synopsis
  145.  *   int v[n], n;
  146.  *   bubble( v, n );
  147.  *
  148.  * description
  149.  *   sorts the first n elements of array v and replaces them in
  150.  *   increasing order.
  151.  *
  152.  * passed
  153.  *   v - the array to be sorted
  154.  *   n - the number of elements of 'v' to be sorted */
  155.  
  156. void bubble( v, n )
  157. int v[], n;
  158.  
  159.     {
  160.     register int i, j, k;
  161.  
  162.     for ( i = n; i > 1; --i )
  163.     for ( j = 1; j < i; ++j )
  164.         if ( v[j] > v[j + 1] )    /* compare */
  165.         {
  166.         k = v[j];    /* exchange */
  167.         v[j] = v[j + 1];
  168.         v[j + 1] = k;
  169.         }
  170.     }
  171.  
  172.  
  173. /* clower - replace upper-case letter to lower-case
  174.  *
  175.  * synopsis:
  176.  *    Char clower();
  177.  *    int c;
  178.  *    c = clower( c );
  179.  */
  180.  
  181. Char clower( c )
  182. register int c;
  183.  
  184.     {
  185.     return ( (isascii( c ) && isupper( c )) ? tolower( c ) : c );
  186.     }
  187.  
  188.  
  189. /* copy_string - returns a dynamically allocated copy of a string
  190.  *
  191.  * synopsis
  192.  *    char *str, *copy, *copy_string();
  193.  *    copy = copy_string( str );
  194.  */
  195.  
  196. char *copy_string( str )
  197. register char *str;
  198.  
  199.     {
  200.     register char *c;
  201.     char *copy;
  202.  
  203.     /* find length */
  204.     for ( c = str; *c; ++c )
  205.     ;
  206.  
  207.     copy = malloc( (unsigned) ((c - str + 1) * sizeof( char )) );
  208.  
  209.     if ( copy == NULL )
  210.     flexfatal( "dynamic memory failure in copy_string()" );
  211.  
  212.     for ( c = copy; (*c++ = *str++); )
  213.     ;
  214.  
  215.     return ( copy );
  216.     }
  217.  
  218.  
  219. /* copy_unsigned_string -
  220.  *    returns a dynamically allocated copy of a (potentially) unsigned string
  221.  *
  222.  * synopsis
  223.  *    Char *str, *copy, *copy_unsigned_string();
  224.  *    copy = copy_unsigned_string( str );
  225.  */
  226.  
  227. Char *copy_unsigned_string( str )
  228. register Char *str;
  229.  
  230.     {
  231.     register Char *c;
  232.     Char *copy;
  233.  
  234.     /* find length */
  235.     for ( c = str; *c; ++c )
  236.     ;
  237.  
  238.     copy = (Char *) malloc( (unsigned) ((c - str + 1) * sizeof( Char )) );
  239.  
  240.     if ( copy == NULL )
  241.     flexfatal( "dynamic memory failure in copy_unsigned_string()" );
  242.  
  243.     for ( c = copy; (*c++ = *str++); )
  244.     ;
  245.  
  246.     return ( copy );
  247.     }
  248.  
  249.  
  250. /* cshell - shell sort a character array in increasing order
  251.  *
  252.  * synopsis
  253.  *
  254.  *   Char v[n];
  255.  *   int n, special_case_0;
  256.  *   cshell( v, n, special_case_0 );
  257.  *
  258.  * description
  259.  *   does a shell sort of the first n elements of array v.
  260.  *   If special_case_0 is true, then any element equal to 0
  261.  *   is instead assumed to have infinite weight.
  262.  *
  263.  * passed
  264.  *   v - array to be sorted
  265.  *   n - number of elements of v to be sorted
  266.  */
  267.  
  268. void cshell( v, n, special_case_0 )
  269. Char v[];
  270. int n, special_case_0;
  271.  
  272.     {
  273.     int gap, i, j, jg;
  274.     Char k;
  275.  
  276.     for ( gap = n / 2; gap > 0; gap = gap / 2 )
  277.     for ( i = gap; i < n; ++i )
  278.         for ( j = i - gap; j >= 0; j = j - gap )
  279.         {
  280.         jg = j + gap;
  281.  
  282.         if ( special_case_0 )
  283.             {
  284.             if ( v[jg] == 0 )
  285.             break;
  286.  
  287.             else if ( v[j] != 0 && v[j] <= v[jg] )
  288.             break;
  289.             }
  290.  
  291.         else if ( v[j] <= v[jg] )
  292.             break;
  293.  
  294.         k = v[j];
  295.         v[j] = v[jg];
  296.         v[jg] = k;
  297.         }
  298.     }
  299.  
  300.  
  301. /* dataend - finish up a block of data declarations
  302.  *
  303.  * synopsis
  304.  *    dataend();
  305.  */
  306.  
  307. void dataend()
  308.  
  309.     {
  310.     if ( datapos > 0 )
  311.     dataflush();
  312.  
  313.     /* add terminator for initialization */
  314.     puts( "    } ;\n" );
  315.  
  316.     dataline = 0;
  317.     datapos = 0;
  318.     }
  319.  
  320.  
  321.  
  322. /* dataflush - flush generated data statements
  323.  *
  324.  * synopsis
  325.  *    dataflush();
  326.  */
  327.  
  328. void dataflush()
  329.  
  330.     {
  331.     putchar( '\n' );
  332.  
  333.     if ( ++dataline >= NUMDATALINES )
  334.     {
  335.     /* put out a blank line so that the table is grouped into
  336.      * large blocks that enable the user to find elements easily
  337.      */
  338.     putchar( '\n' );
  339.     dataline = 0;
  340.     }
  341.  
  342.     /* reset the number of characters written on the current line */
  343.     datapos = 0;
  344.     }
  345.  
  346.  
  347. /* flexerror - report an error message and terminate
  348.  *
  349.  * synopsis
  350.  *    char msg[];
  351.  *    flexerror( msg );
  352.  */
  353.  
  354. void flexerror( msg )
  355. char msg[];
  356.  
  357.     {
  358.     fprintf( stderr, "%s: %s\n", program_name, msg );
  359.  
  360.     flexend( 1 );
  361.     }
  362.  
  363.  
  364. /* flexfatal - report a fatal error message and terminate
  365.  *
  366.  * synopsis
  367.  *    char msg[];
  368.  *    flexfatal( msg );
  369.  */
  370.  
  371. void flexfatal( msg )
  372. char msg[];
  373.  
  374.     {
  375.     fprintf( stderr, "%s: fatal internal error, %s\n", program_name, msg );
  376.     flexend( 1 );
  377.     }
  378.  
  379.  
  380. /* flex_gettime - return current time
  381.  *
  382.  * synopsis
  383.  *    char *flex_gettime(), *time_str;
  384.  *    time_str = flex_gettime();
  385.  *
  386.  * note
  387.  *    the routine name has the "flex_" prefix because of name clashes
  388.  *    with Turbo-C
  389.  */
  390.  
  391. /* include sys/types.h to use time_t and make lint happy */
  392.  
  393. #ifndef MS_DOS
  394. #ifndef VMS
  395. #include <sys/types.h>
  396. #else
  397. #include <types.h>
  398. #endif
  399. #endif
  400.  
  401. #ifdef MS_DOS
  402. #include <time.h>
  403. typedef long time_t;
  404. #endif
  405.  
  406. char *flex_gettime()
  407.  
  408.     {
  409.     time_t t, time();
  410.     char *result, *ctime(), *copy_string();
  411.  
  412.     t = time( (long *) 0 );
  413.  
  414.     result = copy_string( ctime( &t ) );
  415.  
  416.     /* get rid of trailing newline */
  417.     result[24] = '\0';
  418.  
  419.     return ( result );
  420.     }
  421.  
  422.  
  423. /* lerrif - report an error message forma